package com.engc.common.utils;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 * 基础封装类
 * 
 * @author zhixionggu
 * 
 */
public class Record {
	private Map<String, Object> columns = new LinkedHashMap<String, Object>();

	public void setColumns(Map<String, Object> columns) {
		this.columns = columns;
	}

	public Object getColumnValue(String column) {
		return this.columns.get(column);
	}

	public int getInt(String column) {
		return Integer.valueOf(columns.get(column) == null ? "0" : columns.get(
				column).toString());
	}

	public long getLong(String column) {
		return Long.valueOf(columns.get(column) == null ? "0" : columns.get(
				column).toString());
	}

	public boolean getBoolean(String column) {
		return Boolean.valueOf(columns.get(column) == null ? "false" : columns
				.get(column).toString());
	}

	public String getString(String column) {
		return columns.get(column) == null ? "" : columns.get(column)
				.toString();
	}

	public double getDouble(String column) {
		return Double.valueOf(columns.get(column) == null ? "0" : columns.get(
				column).toString());
	}

	public Set<String> getColumns() {
		return columns.keySet();
	}

	public String getInsertSQL(String tableName) {
		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO ");
		sql.append(" " + tableName + " ");
		sql.append(" (");
		Set<String> columnf = columns.keySet();
		Iterator it = columnf.iterator();
		while (it.hasNext()) {
			sql.append(" ").append(it.next()).append(" ").append(",");
		}
		sql.append(")");
		sql.append(" VALUES(");
		for (int i = 0; i < columnf.size(); i++) {
			sql.append("?");
			if (i < columnf.size() - 1) {
				sql.append(",");
			}
		}
		sql.append(") ");
		return sql.toString();
	}
	/**
	 * 获取Record 更新表SQL
	 * @param tableName  表名称
	 * @param sWhere     where 条件
	 * @return
	 */
	public String getUpdateSQL(String tableName, String sWhere) {
		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE ");
		sql.append(" " + tableName + "  ");
		sql.append(" SET ");
		Set<String> columnf = columns.keySet();
		Iterator<String> it = columnf.iterator();
		while (it.hasNext()) {
			sql.append(" ").append(it.next()).append(" ").append("=?")
					.append(",");
		}
		
		sql.deleteCharAt(sql.lastIndexOf(","));
		if (sWhere != null && !"".equals(sWhere)) {
			sql.append(" where ");
			sql.append(sWhere);
		} else {
			sql.append(" where 1=2");
		}
		return sql.toString();
	}
	/**
	 * 获取Record删除语句
	 * @param tableName
	 * @param sWhere
	 * @return
	 */
	public String getDeleteSql(String tableName, String sWhere) {
		// 拼SQL语句
		StringBuffer sql = new StringBuffer();
		sql.append("DELETE FROM ");
		sql.append(" " + tableName + "  ");
		if (sWhere != null && !"".equals(sWhere)) {
			sql.append(" where ");
			sql.append(sWhere);
		} else {
			sql.append(" where 1=2");
		}
		return sql.toString();
	}

	public Record setColumn(String column, Object columnValue) {
		columns.put(column, columnValue);
		return this;
	}
}
